<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/all.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>首页<small>公司分布图</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="分布图" name="first">
                    <!--大地图-->
                    <div id="BigMap" style="width: 1600px;height:700px; margin: 0 auto;"></div>
                </el-tab-pane>
                <el-tab-pane label="详细信息" name="second">
                    <div class="filter-container">
                        <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                                  class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="公司地址" align="center"></el-table-column>
                        <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
                        <el-table-column prop="latitude" label="所在纬度" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                                class="pagiantion"
                                @current-change="handleCurrentChange"
                                :current-page.sync="pagination.currentPage"
                                :page-size="pagination.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pagination.total"
                                v-if="pagination.total != 0">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增地址" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData">
                                <iframe name="right"  src="min_map.html"
                                        style="width:620px;height:640px; margin: 0 auto;"
                                        frameborder="no"></iframe>
                            </el-form>
                        </el-dialog>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=r1XnCPbNZpsUGfyXgTGBzQ1e5XbEzxhy"></script>
<script>
    const vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 5,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '地址名不能为空', trigger: 'blur'}]
            },
            activeName: 'first'
        },
        //钩子函数，VUE对象初始化完成后自动执行
        mounted() {
            this.createMap("BigMap");
            //获取储存的当前分页
            this.pagination.currentPage = this.getContextData("currentPage") || 1;
            this.findPage();
        },
        externals: {
            "BMap": "BMap"
        },
        methods: {
            //给sessionStorage存值
            setContextData: function (key, value) {
                if (typeof value == "string") {
                    sessionStorage.setItem(key, value);
                } else {
                    sessionStorage.setItem(key, JSON.stringify(value));
                }
            },
            // 从sessionStorage取值
            getContextData: function (key) {
                const str = sessionStorage.getItem(key);
                if (typeof str == "string") {
                    try {
                        return JSON.parse(str);
                    } catch (e) {
                        return str;
                    }
                }
                return;
            },
            handleClick(tab, event) {
                console.log(tab, event)
            },
            //分页查询
            findPage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                const param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/address/findPage.do", param).then((res) => {
                    //解析Controller响应回的数据，为模型数据赋值
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                });
            },
            resetForm() {
                this.formData = {};//重置数据
            },
            handleCreate() {
                this.resetForm();
                //弹出新增窗口
                this.dialogFormVisible = true;
                if (this.$refs['dataAddForm'] !== undefined) {
                    this.$refs['dataAddForm'].clearValidate() // 重置验证
                }
            },
            // 删除
            handleDelete(row) {//row其实是一个json对象
                this.$confirm("您确定要删除当前数据吗？", "提示", { //提示框
                    type: 'warning'
                }).then(() => {
                    //确定删除
                    axios.delete("/address/delete/" + row.id + ".do").then((res) => {
                        if (res.data.flag) {
                            //执行成功回显
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                            //重新进行分页查询
                            this.findPage();
                        } else {
                            //执行失败回显
                            this.$message.error(res.data.message);
                        }
                    });
                    //用户点击取消
                }).catch(() => {
                    this.$message({
                        message: '操作已取消',
                        type: 'info'
                    });
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //储存当前页码
                this.setContextData("currentPage", this.pagination.currentPage);
                //重新调用findPage方法进行分页查询
                this.findPage();
            },
            /*初始化地图*/
            createMap(id) {
                axios.get("/address/findAll.do").then(res => {
                    this.dataList = res.data.data;
                    const map = new BMap.Map(id);
                    //map.centerAndZoom(new BMap.Point(114.4285890, 30.4835280), 13);
                    map.centerAndZoom("武汉", 11);//设置地图中心点
                    map.enableScrollWheelZoom(true);
                    let adds = new Array();
                    for (let i = 0; i < this.dataList.length; i++) {
                        adds.push(new BMap.Point(this.dataList[i].longitude, this.dataList[i].latitude));
                    }
                    for (let i = 0; i < adds.length; i++) {
                        const marker = new BMap.Marker(adds[i]);
                        map.addOverlay(marker);
                        marker.setLabel(new BMap.Label(this.dataList[i].name,
                            {offset: new BMap.Size(20, -10)}));
                    }
                })
            }
        }
    });
</script>
</html>